function printOrder(div){
	$(div).printArea();
}

function getFlushParam(windowPanelId) {
  return 'tabId=' + tabpanel.active + '&windowPanelId=' + windowPanelId;
}
function showLoadMask(tabId){
  if($('#'+tabId).length==0) {
    $('#load-mask').show();
    $('#load-mask').fadeTo(0, 0.5);
    $('#load-msg').css('left', (($(document.body).width()/2) - ($('#load-msg').width()/2)));
    $('#load-msg').css('top', (($(document.body).height()/2) - ($('#load-msg').height()/2)));
    $('#load-msg').show();
  }
}
function submitFrameForm(frameId, formId){
  var win ;
  if ($.browser.mozilla) 
	  win = document.getElementById(frameId).contentWindow;
  else 
	  win = window.frames[frameId];
  
  if(win.valid){
	  if(win.valid.validate()) win.document.forms[formId].submit();
  }else
	  win.document.forms[formId].submit();
  
}

var tabpanel;

$(document).ready(function(){
  document.onselectstart = function(){
    return false;
  };
  $('#main').height($(document.body).height() - $('.footer').outerHeight() - $('.header').outerHeight() - $('.module').outerHeight());
  tabpanel = new TabPanel({
    id: 'jTabPanel',
    renderTo: 'main',
    border: false,
    autoResize: true,
    defaultTab: 0,
    icon: 'image/module_icon.gif',
    items: [{
      id: 'workspace',
      title: 'My workspace',
      closable: false,
      html: '<iframe id="workspace" name="workspace" src="" width="100%" height="100%" frameborder="0"></iframe>',
      position: {
        a: '0px -110px',
        b: '-22px -110px'
      }
    }]
  });
  
  addModuleEvent();
  
  var eventArray = [];
  //--------------------------------------------[系统管理]
  eventArray.push({
    trigger: 'dic',
    id: 'dictionary',
    title: '字典管理',
    html: '<iframe src="' + projectName + '/system/dictionary/list.t4m" id="dictionaryFrame" name="dictionaryFrame" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -132px',
      b: '-22px -132px'
    }
  });
  eventArray.push({
    trigger: 'user',
    id: 'users',
    title: '员工管理',
    html: '<iframe id="usersFrame" name="usersFrame" src="' + projectName + '/system/user/listUser.jsp" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px 0px',
      b: '-22px 0px'
    }
  });
  eventArray.push({
    trigger: 'dept',
    id: 'department',
    title: '部门管理',
    html: '<iframe id="departmentFrame" name="departmentFrame" src="' + projectName + '/system/department/listDepartment.jsp" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -66px',
      b: '-22px -66px'
    }
  });
  eventArray.push({
    trigger: 'sys',
    id: 'syslog',
    title: '系统日志',
    html: '<iframe id="syslogFrame" name="syslogFrame" src="' + projectName + '/system/syslog/list.t4m" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -88px',
      b: '-22px -88px'
    }
  });
  eventArray.push({
    trigger: 'mes',
    id: 'sysMesLog',
    title: '系统消息',
    html: '<iframe id="sysMesLogFrame" name="sysMesLogFrame" src="' + projectName + '/system/sysMesUser/listPagination.t4m" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -44px',
      b: '-22px -44px'
    }
  });
  eventArray.push({
    trigger: 'ic',
    id: 'intercom',
    title: '内部通信',
    html: '<iframe id="intercomFrame" name="intercomFrame" src="' + projectName + '/system/intercom/listPagination.t4m" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -22px',
      b: '-22px -22px'
    }
  });
  eventArray.push({
    trigger: 'ro',
    id: 'role',
    title: '角色管理',
    html: '<iframe id="roleFrame" name="roleFrame" src="' + projectName + '/system/role/listPagination.t4m" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -154px',
      b: '-22px -154px'
    }
  });
  eventArray.push({
    trigger: 'b',
    id: 'blog',
    title: '工作日志',
    html: '<iframe src="' + projectName + '/daily/blog/listBlog.jsp" id="blogFrame" name="blogFrame" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -132px',
      b: '-22px -132px'
    }
  });
  eventArray.push({
    trigger: 't',
    id: 'blogTags',
    title: '标签管理',
    html: '<iframe src="' + projectName + '/daily/blogTags/listPagination.t4m" id="blogTagsFrame" name="blogTagsFrame" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -132px',
      b: '-22px -132px'
    }
  });
  
  eventArray.push({
	    trigger: 'proType',
	    id: 'productType',
	    title: '货品分类管理',
	    html: '<iframe id="productTypeFrame" name="productTypeFrame" src="' + projectName + '/productManage/productType/listProductType.jsp" width="100%" height="100%" frameborder="0"></iframe>',
	    position: {
	      a: '0px -66px',
	      b: '-22px -66px'
	    }
  });

  eventArray.push({
	    trigger: 'prod',
	    id: 'product',
	    title: '货品管理',
	    html: '<iframe id="productFrame" name="productFrame" src="' + projectName + '/productManage/product/listProduct.jsp" width="100%" height="100%" frameborder="0"></iframe>',
	    position: {
	      a: '0px -66px',
	      b: '-22px -66px'
	    }
  });
  
  eventArray.push({
	    trigger: 'wareHouseTrigger',
	    id: 'wareHouse',
	    title: '仓库管理',
	    html: '<iframe id="wareHouseFrame" name="wareHouseFrame" src="' + projectName + '/product/warehouse/listPagination.t4m" width="100%" height="100%" frameborder="0"></iframe>',
	    position: {
	      a: '0px -66px',
	      b: '-22px -66px'
	    }
});
  
  eventArray.push({
    trigger: 'customerTrigger',
    id: 'customer',
    title: '客户信息',
    html: '<iframe src="' + projectName + '/sales/customer/listPagination.t4m" id="customerFrame" name="customerFrame" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -132px',
      b: '-22px -132px'
    }
  });
  eventArray.push({
    trigger: 'salesOrderTrigger',
    id: 'salesOrder',
    title: '销售订单',
    html: '<iframe src="' + projectName + '/sales/salesOrder/listPagination.t4m" id="salesOrderFrame" name="salesOrderFrame" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -132px',
      b: '-22px -132px'
    }
  });
  eventArray.push({
	    trigger: 'salesBackOrderTrigger',
	    id: 'salesBackOrder',
	    title: '销售退单',
	    html: '<iframe src="' + projectName + '/sales/salesBackOrder/listPagination.t4m" id="salesBackOrderFrame" name="salesBackOrderFrame" width="100%" height="100%" frameborder="0"></iframe>',
	    position: {
	      a: '0px -132px',
	      b: '-22px -132px'
	    }
	  });
  eventArray.push({
	    trigger: 'salesCountTrigger',
	    id: 'salesCount',
	    title: '销售统计',
	    html: '<iframe src="' + projectName + '/sales/salesCount/listPagination.t4m" id="salesCountFrame" name="salesCountFrame" width="100%" height="100%" frameborder="0"></iframe>',
	    position: {
	      a: '0px -132px',
	      b: '-22px -132px'
	    }
	  });
  eventArray.push({
    trigger: 'supplierTrigger',
    id: 'supplier',
    title: '供应商信息',
    html: '<iframe src="' + projectName + '/purchase/supplier/listPagination.t4m" id="supplierFrame" name="supplierFrame" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -132px',
      b: '-22px -132px'
    }
  });
  eventArray.push({
    trigger: 'stockTrigger',
    id: 'stock',
    title: '库存信息',
    html: '<iframe src="' + projectName + '/product/stock/listPagination.t4m" id="stockFrame" name="stockFrame" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -132px',
      b: '-22px -132px'
    }
  });
  eventArray.push({
	    trigger: 'purchaseOrderTrigger',
	    id: 'purchaseOrder',
	    title: '采购订单',
	    html: '<iframe src="' + projectName + '/purchase/purchaseOrder/listPagination.t4m" id="purchaseOrderFrame" name="purchaseOrderFrame" width="100%" height="100%" frameborder="0"></iframe>',
	    position: {
	      a: '0px -132px',
	      b: '-22px -132px'
	    }
	  });
  eventArray.push({
	    trigger: 'inOrderTrigger',
	    id: 'inOrder',
	    title: '入库单',
	    html: '<iframe src="' + projectName + '/stock/inOrder/listPagination.t4m" id="inOrderFrame" name="inOrderFrame" width="100%" height="100%" frameborder="0"></iframe>',
	    position: {
	      a: '0px -132px',
	      b: '-22px -132px'
	    }
	  },{
		    trigger: 'outOrderTrigger',
		    id: 'outOrder',
		    title: '出库单',
		    html: '<iframe src="' + projectName + '/product/outOrder/listPagination.t4m" id="outOrderFrame" name="outOrderFrame" width="100%" height="100%" frameborder="0"></iframe>',
		    position: {
		      a: '0px -132px',
		      b: '-22px -132px'
		    }
		  });
  eventArray.push({
    trigger: 'arriveOrderTrigger',
    id: 'arriveOrder',
    title: '到货单列表',
    html: '<iframe src="' + projectName + '/purchase/arriveOrder/listPagination.t4m" id="arriveOrderFrame" name="arriveOrderFrame" width="100%" height="100%" frameborder="0"></iframe>',
    position: {
      a: '0px -132px',
      b: '-22px -132px'
    }
  });
  
  for (var i = 0; i < eventArray.length; i++) {
    $('#' + eventArray[i].trigger).click(function(index){
      return function(){
        //showLoadMask(eventArray[index].id);
        tabpanel.addTab({
          id: eventArray[index].id,
          title: eventArray[index].title,
          icon: eventArray[index].icon,
          html: eventArray[index].html,
          position: eventArray[index].position,
          closable: true
        });
      }
    }(i));
  }
});

function addModuleEvent() {
  $('.module-second').width($(document.body).width());
  
  $('.module-third li[id!=""]').hover(function(){
    $(this).addClass('over');
  }, function(){
    $(this).removeClass('over');
  });
  
  $('.od').hover(function(){
    window.clearTimeout($(this).data('timer'));
    if ($('.module-second', this).length === 0) {
      return;
    } else {
      if(!$(this).hasClass('active')) {
        $('.od:visible').removeClass('active');
        $('.module-second:visible').hide();
        $(this).addClass('active');
        $('.module-second', this).show();
      }
    }
  }, function(){
    var _s = $(this);
    var _timer = window.setTimeout(function(){
      _s.removeClass('active');
      $('.module-second', _s).hide();
    }, 500);
    _s.data('timer', _timer);
  });
}

function openChangePassword(){
  new WindowPanel({
    id : 'changePasswordWindow',
    title : '修改密码',
    width : 270,
    height : 125,
    dragable : true,
    html : '<form id="pwForm" class="form"><div class="form-group">请输入您的新密码</div><table class="form-table" cellspacing="0" cellpadding="0"><tr><td class="form-left"><span class="form-required">*</span>旧密码</td><td class="form-right"><input type="password" name="password" class="text"/></td></tr><tr><td class="form-left"><span class="form-required">*</span>新密码</td><td class="form-right"><input type="password" name="passwordRe" class="text"/></td></tr><tr><td class="form-left"><span class="form-required">*</span>确认新密码</td><td class="form-right"><input type="password" name="passwordReRe" class="text"/></td></tr></table></form>',
    tbar : new Toolbar({
	    icon : 'image/op.gif',
	    items : [{
	      type : 'button',
	      text : '修改密码',
	      position : {
	        a: '-60px 0px',
	        b: '-60px -120px'
	      },
	      handler : function(self, toolbar, windowpanel){
	        $.ajax({
	          url : projectName+'/system/ajax/changePassword.t4m',
	          data : $('#pwForm').serialize(),
	          success : function(json){
	            switch(json) {
	              case '1' : alert('密码修改成功！');windowpanel.close();break;
	              case '2' : alert('旧密码输入错误，请重新输入！');break;
	              case '3' : alert('输入的新密码不同，请重新输入！');break;
	            }
	          }
	        });
	      }
	    }]
	  })
  });
}